颤振setState不更新值 您所在的位置:网站首页 seek_set 值 颤振setState不更新值

颤振setState不更新值

2023-05-06 05:06| 来源: 网络整理| 查看: 265

嗨,这里有个飞舞的新手。我有一个通过颤栗提交的项目。这是我的主要代码,当单击按钮时,它不会更新文本以停止。

import 'package:assets_audio_player/assets_audio_player.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:url_launcher/url_launcher_string.dart'; import 'firebase_options.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'FM Mahanama', theme: ThemeData( primaryColor: const Color(0xfffbc02d), primarySwatch: Colors.amber, useMaterial3: true, ), darkTheme: ThemeData( primaryColor: const Color(0xfffbc02d), primarySwatch: Colors.amber, brightness: Brightness.dark, useMaterial3: true, ), themeMode: ThemeMode.system, debugShowCheckedModeBanner: false, home: const MyHomePage(title: 'FM Mahanama'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key, required this.title}) : super(key: key); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _selectedAppIndex = 0; IconData iconPlayStop = Icons.play_arrow_rounded; String txtPlayStop = "Play"; static final assetsAudioPlayer = AssetsAudioPlayer(); @override void initState() { WidgetsFlutterBinding.ensureInitialized(); initFirebaseActivities(); } void initFirebaseActivities() { Firebase.initializeApp(); } static Future initRadioPlayer(param0) async { try { await assetsAudioPlayer.open( Audio.liveStream(param0), showNotification: true, autoStart: true, ); } catch (t) { AlertDialog(title: const Text("Error"), content: Text(t.toString()),); } } void updateButtonText(String txt, IconData iconData){ setState(() { txtPlayStop = txt; iconPlayStop = iconData; }); } @override void dispose() { if(assetsAudioPlayer.isPlaying.value){ assetsAudioPlayer.stop(); } } static void _openFacebookPage() async { String fbProtocolUrl = "fb://page/865683116816630"; String fallbackUrl = "https://www.facebook.com/mcrcofficial/"; try { bool launched = await launchUrlString(fbProtocolUrl); if (!launched) { await launchUrlString(fallbackUrl); } } catch (e) { await launchUrlString(fallbackUrl); } } late final Set _appPages = { Center( child: StreamBuilder( stream: FirebaseFirestore.instance .collection("public") .doc("stream") .snapshots(), builder: (BuildContext context, AsyncSnapshot data) { if (data.hasData) { if (!data.data?.get("onair")) { if (assetsAudioPlayer.isPlaying.value) { assetsAudioPlayer.stop(); } } return Visibility( replacement: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: [ Padding( padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 20.0), child: Image.asset( "assets/images/app_logo.png", width: 200.0, fit: BoxFit.cover, ), ), const Text( "FM Mahanama is currently offline!", style: TextStyle(fontSize: 18.0), ), const Padding( padding: EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 20.0), child: Text( "Checkout our Facebook for page more information"), ), Padding( padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 20.0), child: TextButton.icon( onPressed: () { _openFacebookPage(); }, icon: const Icon(Icons.link_rounded), label: const Text("Facebook Page"), ), ), ], ), ), visible: data.data?.get("onair"), child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: [ Padding( padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 30.0), child: Card( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(30.0)), elevation: 10.0, child: ClipRRect( borderRadius: BorderRadius.circular(30.0), child: Image.network( data.data?.get("cover_img"), width: 300.0, height: 300.0, fit: BoxFit.fitHeight, ), ), ), ), const Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 5.0), child: Text( "Now Playing", style: TextStyle(fontSize: 22.0), ), ), Padding( padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 20.0), child: Text( data.data!.get("nowplaying").toString(), style: const TextStyle( fontSize: 24.0, fontWeight: FontWeight.w600), ), ), const Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 5.0), child: Text( "By", style: TextStyle(fontSize: 18.0), ), ), Padding( padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 30.0), child: Text( data.data!.get("by").toString(), style: const TextStyle( fontSize: 22.0, fontWeight: FontWeight.w600), ), ), ElevatedButton.icon( onPressed: () { if(!assetsAudioPlayer.isPlaying.value) { initRadioPlayer(data.data?.get("link")); updateButtonText("Stop", Icons.stop_rounded); }else{ assetsAudioPlayer.stop(); updateButtonText("Play", Icons.play_arrow_rounded); } }, label: Text(getButtonString(), style: const TextStyle(fontSize: 24.0),), icon: Icon(iconPlayStop, size: 24.0,), ), ], ), ), ); } else { return const Text("There is a error loading data!"); } }, ), ), const Center( child: Text("Scoreboard"), ), const Center( child: Text("About"), ), }; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Center( child: Text(widget.title), ), ), body: Center( child: _appPages.elementAt(_selectedAppIndex), ), bottomNavigationBar: BottomNavigationBar( type: BottomNavigationBarType.fixed, backgroundColor: Colors.transparent, elevation: 0, currentIndex: _selectedAppIndex, selectedFontSize: 14, unselectedFontSize: 14, showUnselectedLabels: false, onTap: (value) { setState(() { _selectedAppIndex = value; }); }, items: const [ BottomNavigationBarItem( label: "Player", icon: Icon(Icons.music_note_rounded), ), BottomNavigationBarItem( label: "Scoreboard", icon: Icon(Icons.scoreboard_rounded), ), BottomNavigationBarItem( label: "About", icon: Icon(Icons.info_rounded), ), ], ), ); } static String getButtonString() { if(assetsAudioPlayer.isPlaying.value){ return "Stop"; }else{ return "Play"; } } }

我想要的是更新按钮文本和图标,停止文本和图标时,按钮被按下,播放器正在播放,反之亦然。

我正在使用最新的颤振与材料设计小部件。我正在为android和ios构建。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有